#!/bin/bash
#libreoffice家目录在第五十五行，要手动修改

### icp安装脚本
#[root@sugon_test ~]# tree /home/admin/asr/newAsr/
#/home/admin/asr/newAsr/
#├── icp-job-2.2.4.2-SNAPSHOT-physical.tar.gz
#├── icp-monitor-physical.tar.gz
#├── icp-services-physical.tar.gz
#├── icp-web-2.2.4.2-SNAPSHOT-physical.tar.gz
#├── io-service-physical.tar.gz
#├── judge-service-physical.tar.gz
#├── judge-web-2.2.4.2-SNAPSHOT-physical.tar.gz
#├── manage-2.2.4.2-SNAPSHOT-physical.tar.gz
#└── simsun.ttc
#
#0 directories, 9 files

### 编辑变量
source_dir=/home/admin/asr/newAsr/				#压缩包所在目录(绝对路径)
target_dir=/opt/				#解压目录(绝对路径)

#配置文件
zookeeper_ip=192.168.101.100:2181			#zookeeper IP地址:端口
zookeeper_net=192.168.101			#zookeeper 网段
group=asr				#服务统一组名

#端口
icpServicePort=20890
judgeServicePort=20888
ioServicePort=20887
icpJobPort=9070
icpMonitorPort=18081
icpWebPort=9081
judgeWebPort=9083
managePort=9088
libreofficePort=8980
#根目录
icpServicesExec=/opt/icp-services/
#libreofficeExec=\/opt\/libreoffice5.4\/
judgeServiceExec=/opt/judge-service/
ioServiceExec=/opt/io-service/
icpJobExec=/opt/icp-job-2.2.4.2-SNAPSHOT/
icpMonitorExec=/opt/icp-monitor/
icpWebExec=/opt/icp-web-2.2.4.2-SNAPSHOT/
judgeWebExec=/opt/judge-web-2.2.4.2-SNAPSHOT/
manageExec=/opt/manage-2.2.4.2-SNAPSHOT/

#解压 "*.tar.gz"
for i in `ls ${source_dir}*.tar.gz`
do
	tar xvf ${i} -C ${target_dir}
done

#icp-service
chmod +x ${icpServicesExec}startup.sh
dos2unix ${icpServicesExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${icpServicesExec}config/application.yml
sed -i "s/port: .*/port: ${icpServicePort}/g" ${icpServicesExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${icpServicesExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${icpServicesExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${icpServicesExec}config/application.yml

dos2unix ${icpServicesExec}config/application.yml
dos2unix ${icpServicesExec}config/bootstrap.properties

#judge-services
#libreoffice
echo -ne "#### 安装libreoffice"
yum -y install ${target_dir}LibreOffice_5.4.7.2_Linux_x86-64_rpm/RPMS/*.rpm >> /install.log 2>&1
yum -y install libSM-1.2.2-2.el7.x86_64 --setopt=protected_multilib=false >> /install.log 2>&1
yum -y install cups-libs cairo >> /install.log 2>&1
#安装字体
mkdir /home/font && mkdir /usr/share/fonts/ && mkdir /opt/temp
cp -a ${source_dir}simsun.ttc /usr/share/fonts/
chmod 644 /usr/share/fonts/simsun.ttc
yum -y install mkfontscale fc-cah && mkfontscale && fc-cache -fv >> /install.log 2>&1   #更新字体缓存
#验证
cd /opt/libreoffice5.4/program/ && ./soffice --headless --accept="socket,host=127.0.0.1,port=$libreofficePort,tcpNoDelay=1;urp;StarOffice.ServiceManager" --nofirststartwizard &
ps -ef|grep nofirststartwizard  > /dev/null 2>&1 && echo "... OK" || echo "... ERROR"

chmod +x ${judgeServiceExec}startup.sh
dos2unix ${judgeServiceExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${judgeServiceExec}config/application.yml
sed -i "s/port: .*/port: ${judgeServicePort}/g" ${judgeServiceExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${judgeServiceExec}config/application.yml
sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${judgeServiceExec}config/application.yml
sed -i "s/fontPath:.*/fontPath: \/usr\/share\/fonts\/simsun.ttc,1/g" ${judgeServiceExec}config/application.yml
sed -i "s/tempFileAddress:.*/tempFileAddress: \/opt\/temp/g" ${judgeServiceExec}config/application.yml
sed -i "s/officeHome:.*/officeHome: \/opt\/libreoffice5.4\//g" ${judgeServiceExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${judgeServiceExec}config/bootstrap.properties

dos2unix ${judgeServiceExec}config/application.yml
dos2unix ${judgeServiceExec}config/bootstrap.properties

#io-services
chmod +x ${ioServiceExec}startup.sh
dos2unix ${ioServiceExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${ioServiceExec}config/application.yml
sed -i "s/port: .*/port: ${ioServicePort}/g" ${ioServiceExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${ioServiceExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${ioServiceExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${ioServiceExec}config/application.yml

dos2unix ${ioServiceExec}config/application.yml
dos2unix ${ioServiceExec}config/bootstrap.properties

#icp-job
chmod +x ${icpJobExec}startup.sh
dos2unix ${icpJobExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${icpJobExec}config/application.yml
sed -i "0,/port:.*/s/port:.*/port: ${icpJobPort}/" ${icpJobExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${icpJobExec}config/application.yml
sed -i "s/serverLists: .*/serverLists: ${zookeeper_ip}/g" ${icpJobExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${icpJobExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${icpJobExec}config/application.yml

dos2unix ${icpJobExec}config/application.yml
dos2unix ${icpJobExec}config/bootstrap.properties

#icp-monitor
chmod +x ${icpMonitorExec}startup.sh
dos2unix ${icpMonitorExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${icpMonitorExec}config/application.yml
sed -i "0,/port:.*/s/port:.*/port: ${icpMonitorPort}/" ${icpMonitorExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${icpMonitorExec}config/application.yml
sed -i "s/segment: .*/segment: ${zookeeper_net}/g" ${icpMonitorExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${icpMonitorExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${icpMonitorExec}config/application.yml

dos2unix ${icpMonitorExec}config/application.yml
dos2unix ${icpMonitorExec}config/bootstrap.properties

#icp-web
mkdir /opt/temp
chmod +x ${icpWebExec}startup.sh
dos2unix ${icpWebExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${icpWebExec}config/application.yml
sed -i "0,/port:.*/s/port:.*/port: ${icpWebPort}/" ${icpWebExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${icpWebExec}config/application.yml
sed -i "s/temp: .*/temp: \/opt\/temp/g" ${icpWebExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${icpWebExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${icpWebExec}config/application.yml

dos2unix ${icpWebExec}config/application.yml
dos2unix ${icpWebExec}config/bootstrap.properties

#judge-web
chmod +x ${judgeWebExec}startup.sh
dos2unix ${judgeWebExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${judgeWebExec}config/application.yml
sed -i "0,/port:.*/s/port:.*/port: ${judgeWebPort}/" ${judgeWebExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${judgeWebExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${judgeWebExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${judgeWebExec}config/application.yml

dos2unix ${judgeWebExec}config/application.yml
dos2unix ${judgeWebExec}config/bootstrap.properties

#manage
chmod +x ${manageExec}startup.sh
dos2unix ${manageExec}startup.sh
sed -i "s/address: .*/address: ${zookeeper_ip}/g" ${manageExec}config/application.yml
sed -i "0,/port:.*/s/port:.*/port: ${managePort}/" ${manageExec}config/application.yml
sed -i "s/group: .*/group: ${group}/g" ${manageExec}config/application.yml
sed -i "s/peche.zookeeper.connectionString=.*/peche.zookeeper.connectionString=${zookeeper_ip}/g" ${manageExec}config/bootstrap.properties
#sed -i "s/uploadPosition:.*/uploadPosition: 1/g" ${manageExec}config/application.yml

dos2unix ${manageExec}config/application.yml
dos2unix ${manageExec}config/bootstrap.properties
